import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';

/// @Title   自定义Text（1：使用自定义字体，2：去除上下边距，3：新增支持行间距）
/// @Author: bryce
/// @CreateDate: 2023年10月16日16:07:12
class GTText extends StatelessWidget {
  final String text;
  final TextAlign? textAlign;
  final TextDirection? textDirection;
  final TextStyle? style;
  final double? fontSize;
  final Color? textColor;
  final FontWeight? fontWeight;
  final TextOverflow? overflow;
  final int? maxLines;
  final Color? selectionColor;
  final FontStyle? fontStyle;
  final StrutStyle? strutStyle;
  final Locale? locale;
  final bool? softWrap;
  final double? textScaleFactor;
  final String? semanticsLabel;
  final TextWidthBasis? textWidthBasis;

  /// 行间距数值 默认4
  final double lineSpace;

  /// 是否移除默认的auto上下边距 默认false不去除
  final bool removeTopBottomPadding;
  final EdgeInsetsGeometry padding;

  const GTText(this.text,
      {super.key,
      this.textAlign,
      this.textDirection,
      this.style,
      this.fontSize,
      this.textColor,
      this.fontWeight,
      this.overflow,
      this.maxLines,
      this.fontStyle,
      this.lineSpace = 4,
      this.selectionColor,
      this.strutStyle,
      this.locale,
      this.softWrap,
      this.textScaleFactor,
      this.semanticsLabel,
      this.textWidthBasis,
      this.removeTopBottomPadding = false,
      this.padding = EdgeInsets.zero})
      : assert(
          style != null || fontSize != null,
          'please set textStyle or fontSize',
        );

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: padding,
      child: Text(
        text,
        textAlign: textAlign,
        textDirection: textDirection,
        overflow: overflow,
        maxLines: maxLines,
        selectionColor: selectionColor,
        style: getInitTextStyle(context, style, fontSize, textColor, fontWeight, fontStyle, lineSpace),
        locale: locale,
        softWrap: softWrap,
        textScaleFactor: textScaleFactor,
        semanticsLabel: semanticsLabel,
        textWidthBasis: textWidthBasis,
        textHeightBehavior: TextHeightBehavior(
            applyHeightToLastDescent: !removeTopBottomPadding, applyHeightToFirstAscent: !removeTopBottomPadding),
        // strutStyle: StrutStyle(fontSize: mFontSize, leading: (lineSpace / mFontSize)),
      ),
    );
  }
}

/// 初始化TextStyle
TextStyle getInitTextStyle(
  BuildContext? context,
  TextStyle? style,
  double? fontSize,
  Color? textColor,
  FontWeight? fontWeight,
  FontStyle? fontStyle,
  double lineSpace,
) {
  TextStyle mStyle = (style ??
          (null == context
              ? gtTextStyle(fontWeight: fontWeight ?? FontWeight.w400)
              : DefaultTextStyle.of(context).style))
      .copyWith(
    fontFamily: 'GTFontFamily',
    package: 'lib_uikit',
  );

  if (null != fontSize) {
    mStyle = mStyle.copyWith(fontSize: fontSize);
  }

  if (null != textColor) {
    mStyle = mStyle.copyWith(color: textColor);
  }

  if (null != fontWeight) {
    mStyle = mStyle.copyWith(fontWeight: fontWeight);
  }
  if (null != fontStyle) {
    mStyle = mStyle.copyWith(fontStyle: fontStyle);
  }
  mStyle = mStyle.copyWith(height: (lineSpace / (mStyle.fontSize ?? 16)) + 1);
  return mStyle;
}
